import { createStore } from 'vuex'
import VuexPersistence from 'vuex-persist'
import getters from './getters'

const vuexLocal = new VuexPersistence({
  key: 'vuex',
  storage: window.sessionStorage
})
// const modulesFiles = require.context('./modules', true, /\.js$/)

// const modules = modulesFiles.keys().reduce((modules, modulePath) => {
//   // set './app.js' => 'app'
//   const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
//   const value = modulesFiles(modulePath)
//   modules[moduleName] = value.default
//   return modules
// }, {})

const modulesFiles = import.meta.globEager("./modules/*.js")
const modules= {};
for (const key in modulesFiles) {
  if (Object.prototype.hasOwnProperty.call(modulesFiles, key)) {
      modules[key.replace(/^\.\/(.*)\.\w+$/, '$1').replace(/modules\//, '')] = modulesFiles[key].default
  }
}
const store = createStore({
  modules,
  getters,
  plugins: [vuexLocal.plugin]
})

export default store
